package com.szrt.department.controller;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.szrt.department.entity.DepartmentEntity;
import com.szrt.department.service.DepartmentService;
import com.szrt.department.service.impl.DepartmentServiceImpl;

public class DepartmentController extends HttpServlet {

	private static final long serialVersionUID = -2248383061552356323L;
	
	
	//套路：把dao申明成此类1个属性   (service也是如此）
	private DepartmentService service = new DepartmentServiceImpl();
	
	@Override
	protected void doPost(HttpServletRequest request, HttpServletResponse response) 
			throws ServletException, IOException {
		
		//1 设置乱码
		request.setCharacterEncoding("utf-8");
		
		//2 拿到页面传过来的值
		String departmentCode = request.getParameter("departmentCode");
		String departmentName = request.getParameter("departmentName");
		
		//3把这个东西放到作用域requestScope
		DepartmentEntity entity = new DepartmentEntity();
		entity.setDepartmentCode(Integer.parseInt(departmentCode));
		entity.setDepartmentName(departmentName);
		
		/**
		 * 1 有异常      counter = -1
		 * 2 没有重复   counter = 0
		 * 3 重复         counter >0
		 */
		//4把这个数据放到数据库中...
		int counter = service.add(entity);
		
		String url ="";
		if(counter ==1){
			//说明成功
			url ="/department/success.jsp";
		}else{
			url ="/department/fail.jsp";
		}
		
		
		//5给出提示
		request.getRequestDispatcher(url).forward(request, response);;
		
		
	}

}
